// Author: Albert Wu
// Filename: routeLibrary.js
// Assignment: Palo Alto Bicycle Club
// Date Created: 6/8/09
// Javascript for displaying route library

// Route library file URL
var libraryURL = "./data/routeLibrary.json"; 

// Loads route library from JSON file as collapsible list
function getRouteLibrary() {
  $("#library").append("<p><br />Ratings Key: Speed (A-F) / Hilliness (1-4) / Mileage, with A being the slowest and 1 being the flatest.")
                       .append("<p><br /><a href='./data/Route Library.zip'>Download Route Library")
                       .append("<ul id='routeList'>");
  
  $.getJSON(libraryURL, function(library) {
    $.each(library, function(i, route) {
      var header = route.name + " - "  + route.rating.speed + "/" + route.rating.hilliness + "/" + route.rating.mileage;
      var directList = $("<ul>").attr("id", i).css({"list-style-type": "none"});
      var directions = "";
        
      $("<li>").append(header).append(directList).appendTo("#routeList");
        
      $.each(route.directions, function(j, direction) {
        directions += direction + "<br />";         
      });
        
      $("<li>").append("<p><br />" + directions + "<br /><p>").appendTo(directList);
    });
      
    setCollapsibility();
  });
}

// Sets list collapsibility
function setCollapsibility() {
  $("li:has(ul)").click(function(event) {
    if (this == event.target) {
      $(this).css("list-style-image", ($(this).children().is(":hidden")) ?  "url(./images/minus.gif)" : "url(./images/plus.gif)"); 
      $(this).children().toggle("slow");
    }   
  }).css({"list-style-image": "url(./images/plus.gif)"}).css({"cursor": "pointer"}).children().hide();
        
  $("li:not(:has(ul))").css({"list-style-image": "none"}); 
}

